﻿using ASmile;
using Microsoft.AspNetCore.Http;
using System;
using System.Threading.Tasks;

namespace ASmile.Web
{
    public class GlobalExceptionMiddleware
    {
        private readonly RequestDelegate next;

        public GlobalExceptionMiddleware(RequestDelegate next)
        {
            this.next = next;
        }

        //public void Invoke(HttpContext context)
        //{ 
            
        //}

        public async Task Invoke(HttpContext context)
        {
            try
            {
                await next.Invoke(context);
            }
            catch (Exception ex)
            {
                string ip = context.Connection.RemoteIpAddress.ToString();
                LogHelper.LogObj.Fatal($"IP:{ip},发生错误 ", ex);
            }
        }

        //private async Task HandleExceptionAsync(HttpContext context, Exception e)
        //{
        //    string ip = context.Connection.RemoteIpAddress.ToString();
        //    LogHelper.LogObj.Fatal($"IP:{ip},发生错误 ", ex);
        //}
    }
}
